package com.leoman.plz_p.views.homepage;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.leoman.plz_p.R;
import com.leoman.plz_p.adapter.MainClassAdapter;
import com.leoman.plz_p.interfaces.FineCourseView;
import com.leoman.plz_p.module.bean.CourseBean;
import com.leoman.plz_p.presenter.FineCoursePresenter;
import com.leoman.plz_p.weidget.NavBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.List;

import butterknife.Bind;
import cn.com.corelib.base.BaseActivity;
import cn.com.corelib.utils.ToastMgr;

/**
 * Created by guoh on 2018/9/2.
 */
public class FineCourseActivity extends BaseActivity<FineCourseView,FineCoursePresenter> implements FineCourseView {

    public static Intent getLaunchIntent(Context context) {
        Intent intent = new Intent(context, FineCourseActivity.class);
        return intent;
    }

    @Bind(R.id.nav)
    NavBar nav;
    @Bind(R.id.gv_course)
    ListView gvCourse;
    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.ptr_layout)
    SmartRefreshLayout ptrLayout;

    private MainClassAdapter qualityClassAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_hot_search;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        nav.setTitle("精品课程");
        qualityClassAdapter = new MainClassAdapter(getViewContext());
        gvCourse.setAdapter(qualityClassAdapter);
        gvCourse.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(CourseDetailActivity.getLaunchIntent(getViewContext(), qualityClassAdapter.getItem(position)));
            }
        });
        getPresenter().getQualityCourse(getText(etSearch),true);
        ptrLayout.setEnableLoadmore(true);
        ptrLayout.setEnableRefresh(true);
        ptrLayout.setEnableLoadmoreWhenContentNotFull(false);
        ptrLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                getPresenter().getQualityCourse(getText(etSearch),false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                getPresenter().getQualityCourse(getText(etSearch),true);
            }
        });
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView text, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    getPresenter().getQualityCourse(getText(etSearch),true);
                }
                return false;
            }
        });
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(TextUtils.isEmpty(s)){
                    getPresenter().getQualityCourse(getText(etSearch),true);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected FineCoursePresenter createPresenter() {
        return new FineCoursePresenter();
    }

    @Override
    public void obtainNoMoreData() {
        ptrLayout.finishLoadmoreWithNoMoreData();
    }

    @Override
    public void obtainQualityCourseList(List<CourseBean> rows, boolean reload) {
        if (reload) {
            qualityClassAdapter.replaceAll(rows);
            ptrLayout.finishRefresh(true);
        } else {
            qualityClassAdapter.addAll(rows);
            ptrLayout.finishLoadmore(true);
        }
    }

    @Override
    public void obtainNoData() {
        qualityClassAdapter.clear();
        ToastMgr.show("没有找到课程");
    }
}
